package com.lovo.list.game;

import java.lang.reflect.Constructor;
import java.util.*;

public class Doudizhu {
    private ArrayList<Card> list = new ArrayList();
    private String[] colors = {"♠", "♥", "♣", "♦"};
    private String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//    private String pokevaule = "";

    public static void main(String[] args) {
        Doudizhu d = new Doudizhu();
        d.init();
        d.xipai();
        d.fapai();
    }

    // 初始化54张扑克牌
    public void init() {
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < colors.length; j++) {
//              pokevaule = colors[j] + num[i];
                list.add(new Card(colors[j], num[i]));
            }
        }
        list.add(new Card("", "小王"));
        list.add(new Card("", "大王"));
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(list);
    }

    // 发牌
    public void fapai() {
        //sublist
        System.out.println("玩家一：" + sort(list.subList(0, 17)));
        System.out.println("玩家二：" + sort(list.subList(17, 34)));
        System.out.println("玩家三：" + sort(list.subList(34, 51)));
        System.out.println("底牌：" + list.subList(51, 54));
    }

    // 排序
    public List sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                int index1 = -1;
                int index2 = -1;
                if (o1.getPoint().equals("大王")) {
                    index1 = 15;
                } else if (o1.getPoint().equals("小王")) {
                    index1 = 14;
                }
                if (o2.getPoint().equals("大王")) {
                    index2 = 15;
                } else if (o2.getPoint().equals("小王")) {
                    index2 = 14;
                }
                for (int i = 0; i < num.length; i++) {
                    if (o1.getPoint().equals(num[i])) {
                        index1 = i;
                    }
                    if (o2.getPoint().equals(num[i])) {
                        index2 = i;
                    }
                }
                return index1 - index2;
            }
        });
        return list;
    }
}
